﻿using System;
using System.Web;
using System.Web.UI;
using MyHealthByDesign.Helper;

namespace MyHealthByDesign
{
    /// <summary>
    /// Description:
    /// Displays website default page
    /// 
    /// Author: Sanjeev Shrestha
    /// Date Created: 10/17/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// 10/19/2011 Sanjeev Shrestha - add logic for error message display.
    /// 10/20/2011 Sanjeev Shrestha - change logic for user id.
    /// 11/01/2001 Sanjeev Shrestha - change redirect
    /// 
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
        Domain.Domain domain = new Domain.Domain();

        /// <summary>
        /// Page Load event handler 
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        ///None
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Submit button event handler - check if credentials are valid and login
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string email = txtEmail.Text;
            string password = txtPassword.Text;

            if (domain.IsValidUser(email, password))
            {
                Response.Redirect("Views/MyHealthData.aspx");
            }

            DisplayErrorMessage("Invalid email or password. Please try again!");
        }

        /// <summary>
        /// DisplayErrorMessage - displays error if needed 
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Error is displayed if necessary
        /// </summary>
        /// <param name="message"></param>
        public static void DisplayErrorMessage(string message)
        {
            var currentPage = HttpContext.Current.Handler as Page;
            if (currentPage != null) currentPage.Validators.Add(new ValidationError(message));
        }
    }
}